package com.aishua.fire.common.eenum;

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

/**
 * 白名单接口列表
 * @author Administrator
 *
 */
@Slf4j
public enum LogBlackRespEnum {

	/** 配置类接口 **/
	getUnreadMsg("/findUnReadedPriv2Messages","GET","查询_未读站内消息列表(头部)"),
	getUnreadMsg2("/findUnReadedPriv1Messages","GET","查询_未读站内消息列表(底部)");
	

	/** 接口名 **/
	private String interfaceName;
	/** 方法名 **/
	private String methodName;
	/** 描述 **/
	private String desc;
	
	LogBlackRespEnum(String interfaceName, String methodName, String desc){
		this.interfaceName = interfaceName;
		this.methodName = methodName;
		this.desc = desc;
	}

	public String getInterfaceName() {
		return interfaceName;
	}

	public void setInterfaceName(String interfaceName) {
		this.interfaceName = interfaceName;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public static boolean isInBlackList(HttpServletRequest request) {
		String method = request.getMethod();
		String uriVal = request.getRequestURI();
//		log.info("当前请求的uriVal:{}+{}",uriVal,method);
		for(LogBlackRespEnum blackEnum : values()) {
			String reqMehod = blackEnum.getMethodName();
//			log.info("遍历的方法名:{}",reqMehod);
			if(reqMehod.equalsIgnoreCase(method)) {
				String reqUrl = blackEnum.getInterfaceName();
//				log.info("遍历相同的method的uri:{},",reqUrl);
				/*if(reqUrl.equals(uriVal)) {
					return true;
				}*/
				String regex = "\\{[^\\}]+\\}";
				String uriRegex = reqUrl.replaceAll(regex, "[^\\\\}]+");
				if(Pattern.matches(uriRegex, uriVal)) {
					return true;
				}
			}
		}
		return false;
	}

	
}
